package com.phb.puhuibao.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idp.pub.entity.Pager;
import com.idp.pub.service.IBaseService;
import com.idp.pub.web.controller.BaseController;
import com.phb.puhuibao.entity.Comment;
import com.phb.puhuibao.entity.CommentReply;
@Controller
@RequestMapping(value = "/comment")
public class CommentController extends BaseController<Comment, String> {
	@Resource(name = "jdbcTemplate")
	private JdbcTemplate jdbcTemplate;

	@Resource(name = "commentService")
	public void setBaseService(IBaseService<Comment, String> baseService) {
		super.setBaseService(baseService);
	}


	@Resource(name = "commentReplyService")
	private IBaseService<CommentReply, String> commentReplyService;

	 //-----评论列表-----
	@RequestMapping(value="query")
	@ResponseBody
	public Map<String, Object> query(@RequestParam String jobId) {
		Map<String, Object> data = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		try {
			params.put("jobId", jobId);
			params.put("commentOrReply", 0);
			params.put("orderBy", "create_time");
			params.put("order", "asc");		
			List <Comment> p = this.getBaseService().findList(params);
			for (Comment comment : p) {
					params = new HashMap<String, Object>();
					params.put("commentOrReply", comment.getComId());
					params.put("orderBy", "create_time");
					params.put("order", "asc");	
					List<Comment> list = this.getBaseService().findList(params);
					comment.setReplyList(list);
				    
			}
			data.put("result", p);
			data.put("count", p.size());
			data.put("message", "查询评论列表成功！");
			data.put("status", 1);
		
		} catch (Exception e) {
			// TODO: handle exception
			data.put("message", "查询评论列表失败！");
			data.put("status", 0);
		}
		return data;
	}
	//-----我要评论-----
			@RequestMapping(value="commentByUser")
			@ResponseBody
			public Map<String, Object> commentByUser(@RequestParam String jobId ,@RequestParam String userId ,
					                                 @RequestParam String content) {
				Map<String, Object> data=new HashMap<String, Object>();
				try {
					Comment comment = new Comment();
					comment.setJobId(Integer.parseInt(jobId));
					comment.setUserId(Integer.parseInt(userId));				
					comment.setCommentOrReply(0);
					comment.setContent(content);
					this.getBaseService().save(comment);
					data.put("message", "评论保存成功！");
					data.put("status", 0);
				} catch (Exception e) {
					data.put("message", "评论失败啦！");
					data.put("status", 0);					
				}
				return data;
			}
	//-----删除评论-----
	@RequestMapping(value="deleteComment")
	@ResponseBody
	public Map<String, Object> deleteComment(@RequestParam int comId) {
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("comId", comId);
		int count = this.getBaseService().delete(params);
		Map<String, Object> data = new HashMap<String, Object>();
		if (count == 0) {
			data.put("message", "评论删除失败!");
			data.put("status", 0);
		} else {
			data.put("message", "评论删除成功!");			
			data.put("status", 1);
		}
		return data;
	}

		
}

